home *** CD-ROM | disk | FTP | other *** search
/ PC Open 97 / PC Open 97 CD2.bin / Demo / FileMaker / FileMaker Pro Prova / Files / Data1.cab / subsummary.xsl4 < prev    next >
Encoding:
Extensible Markup Language  |  2002-05-24  |  11.4 KB  |  344 lines

  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fmp="http://www.filemaker.com/fmpxmlresult" exclude-result-prefixes="fmp">
  2.     <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
  3.     <!--
  4. File: subsummary.xsl
  5.  
  6. Transforms data exported in the FMPXMLRESULT grammar into an 
  7. HTML table. Data is grouped according to values in the first
  8. exported field. Members of each group are sorted by the second
  9. field in the export.
  10.  
  11. The last field in the export is totaled if its data type is NUMBER,
  12. otherwise the number of items in each group is counted.
  13.  
  14. In other words, data export in this manner:
  15.  
  16. Group   Last Name   First Name   Score
  17. =======================================
  18. Red     Smith       Jim          10
  19. Red     Doe         John         20
  20. Blue    Woods       Bill         12
  21. Blue    Hope        Sue          17
  22. Red     Ringer      Bob          18
  23.  
  24. Is presented this way:
  25.  
  26. Group   Last Name   First Name   Score
  27. =======================================
  28. Blue    Hope        Sue          17
  29.         Woods       Bill         12
  30.                     Total        29
  31.  
  32. Red     Doe         John         20
  33.         Smith       Jim          10
  34.         Ringer      Bob          18
  35.                     Total        48
  36.               Grand Total        77
  37.  
  38. ===============================================================
  39.  
  40. Copyright ┬⌐ 2002 FileMaker, Inc.
  41. All rights reserved.
  42.  
  43. Redistribution and use in source and binary forms, with or
  44. without modification, are permitted provided that the following
  45. conditions are met:
  46.  
  47. * Redistributions of source code must retain the above copyright
  48.   notice, this list of conditions and the following disclaimer.
  49.  
  50. * Redistributions in binary form must reproduce the above copyright
  51.   notice, this list of conditions and the following disclaimer in 
  52.   the documentation and/or other materials provided with the
  53.   distribution.
  54.  
  55. * Neither the name of the FileMaker, Inc. nor the names of its 
  56.   contributors may be used to endorse or promote products derived
  57.   from this software without specific prior written
  58.   permission.
  59.  
  60. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  61. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  62. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  63. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
  64. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
  65. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  66. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  67. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  68. BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  69. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  70. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  71. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  72.     
  73. ===============================================================
  74. -->
  75. <!--
  76. Use the Meunchian method to group the exported data based on data 
  77. in the first field.
  78. -->
  79.     <xsl:key name="groupKey" match="fmp:FMPXMLRESULT/fmp:RESULTSET/fmp:ROW" use="fmp:COL[1]/fmp:DATA"/>
  80. <!--
  81. Same idea, stuff the database name into a variable for use elsewhere.
  82. -->
  83.     <xsl:variable name="dbName">
  84.         <xsl:value-of select="fmp:FMPXMLRESULT/fmp:DATABASE/@NAME"/>
  85.     </xsl:variable>
  86. <!--
  87. Change these variables to alter the appearance of the table.
  88. -->
  89.     <xsl:variable name="rowColor">
  90.         <xsl:value-of select="'#cccccc'"/>
  91.     </xsl:variable>
  92.     <xsl:variable name="groupColor">
  93.         <xsl:value-of select="'#999999'"/>
  94.     </xsl:variable>
  95.     <xsl:variable name="headerColor">
  96.         <xsl:value-of select="'#9999cc'"/>
  97.     </xsl:variable>
  98.     <xsl:variable name="footerColor">
  99.         <xsl:value-of select="'#9999cc'"/>
  100.     </xsl:variable>
  101. <!--
  102. Count all the children in the METADATA block, this gives us the
  103. number of columns in the database.
  104. -->
  105.     <xsl:variable name="dbNumCols">
  106.         <xsl:value-of select="count(fmp:FMPXMLRESULT/fmp:METADATA/child::*) "/>
  107.     </xsl:variable>
  108.     
  109. <!--
  110. The data type of the last column in the export determines whether 
  111. or not the data is counted or totaled.
  112. -->
  113.     <xsl:variable name="type">
  114.         <xsl:value-of select="fmp:FMPXMLRESULT/fmp:METADATA/fmp:FIELD[last()]/@TYPE"/>
  115.     </xsl:variable>
  116.     <!--
  117.     -->
  118.     <xsl:template match="fmp:FMPXMLRESULT">
  119.         <html>
  120.             <head>
  121.                 <title>
  122.                     <xsl:value-of select="$dbName"/>
  123.                 </title>
  124.             </head>
  125.             <body>
  126.                 <table cellSpacing="1" cellPadding="3" width="100%" border="0">
  127.                     <xsl:call-template name="tableHeader"/>
  128. <!--
  129. For every unique value in column one, create a new group
  130. -->
  131.                     <xsl:for-each select="fmp:RESULTSET/fmp:ROW[generate-id(.)=generate-id(key('groupKey', fmp:COL[1]/fmp:DATA)[1])]">
  132. <!--
  133. Sort the groups
  134. -->
  135.                         <xsl:sort select="fmp:COL[1]/fmp:DATA" data-type="text"/>
  136.                         <xsl:variable name="currentGroup" select="key('groupKey', fmp:COL[1]/fmp:DATA)"/>
  137.                         <xsl:for-each select="$currentGroup">
  138. <!--
  139. Sort the members of each group by column two
  140. -->
  141.                             <xsl:sort select="fmp:COL[2]/fmp:DATA" data-type="text"/>
  142.                             <xsl:if test="position()=1">
  143.                                 <xsl:call-template name="groupRow"/>
  144.                             </xsl:if>
  145.                             <xsl:call-template name="makeRow"/>
  146.                             <xsl:if test="position()=count($currentGroup)">
  147.                                 <xsl:call-template name="subSumRow"/>
  148.                             </xsl:if>
  149.                         </xsl:for-each>
  150.                     </xsl:for-each>
  151.                     <tr>
  152.                         <xsl:choose>
  153.                             <xsl:when test="$type = 'NUMBER'">
  154. <!--
  155. A field of type NUMBER results in totals
  156. -->
  157.                                 <td align="right" valign="middle">
  158.                                     <xsl:attribute name="bgcolor"><xsl:value-of select="$footerColor"/></xsl:attribute>
  159.                                     <xsl:attribute name="colspan"><xsl:value-of select="$dbNumCols - 1"/></xsl:attribute>
  160.                                     <font size="4">
  161.                                         <xsl:text>Grand Total:</xsl:text>
  162.                                     </font>
  163.                                 </td>
  164.                                 <td align="middle" valign="middle">
  165.                                     <xsl:attribute name="bgcolor"><xsl:value-of select="$footerColor"/></xsl:attribute>
  166.                                     <font size="4">
  167.                                         <xsl:value-of select="sum(fmp:RESULTSET/fmp:ROW/fmp:COL[last()]/fmp:DATA)"/>
  168.                                     </font>
  169.                                 </td>
  170.                             </xsl:when>
  171.                             <xsl:otherwise>
  172. <!--
  173. Otherwise, simply count the number of items in each group
  174. and total them
  175. -->
  176.                                 <td align="right" valign="middle">
  177.                                     <xsl:attribute name="bgcolor"><xsl:value-of select="$footerColor"/></xsl:attribute>
  178.                                     <xsl:attribute name="colspan"><xsl:value-of select="$dbNumCols - 1"/></xsl:attribute>
  179.                                     <font size="4"/>
  180.                                     <xsl:text>Total Count:</xsl:text>
  181.                                 </td>
  182.                                 <td align="middle" valign="middle">
  183.                                     <xsl:attribute name="bgcolor"><xsl:value-of select="$footerColor"/></xsl:attribute>
  184.                                     <font size="4">
  185.                                         <xsl:value-of select="count(fmp:RESULTSET/fmp:ROW/fmp:COL[1]/fmp:DATA)"/>
  186.                                     </font>
  187.                                 </td>
  188.                             </xsl:otherwise>
  189.                         </xsl:choose>
  190.                     </tr>
  191.                 </table>
  192.             </body>
  193.         </html>
  194.     </xsl:template>
  195. <!--
  196. Template: tableHeader
  197.     
  198. The database name spans the entire table.
  199. Individual field names head each column.
  200. -->
  201.     <xsl:template name="tableHeader">
  202.         <tr>
  203.             <th height="50" align="left" valign="middle">
  204.                 <xsl:attribute name="bgcolor"><xsl:value-of select="$headerColor"/></xsl:attribute>
  205.                 <xsl:attribute name="colspan"><xsl:value-of select="$dbNumCols"/></xsl:attribute>
  206.                 <font size="4">
  207.                     <DIV align="center">
  208.                         <xsl:value-of select="fmp:DATABASE/@NAME"/>
  209.                     </DIV>
  210.                 </font>
  211.             </th>
  212.         </tr>
  213.         <tr>
  214.             <xsl:for-each select="/*/*/fmp:FIELD">
  215.                 <th align="middle" valign="middle">
  216.                     <xsl:attribute name="bgcolor"><xsl:value-of select="$headerColor"/></xsl:attribute>
  217.                     <font size="4">
  218.                         <xsl:value-of select="@NAME"/>
  219.                     </font>
  220.                 </th>
  221.             </xsl:for-each>
  222.         </tr>
  223.     </xsl:template>
  224. <!--
  225. Template: makeRow
  226.     
  227. Builds the individual table rows.
  228. Center the data in the last cell if the field is a number.
  229. Add spaces to the output to move the data away from table
  230. cell borders.
  231. -->
  232.     <xsl:template name="makeRow">
  233.         <tr>
  234.             <td valign="middle">
  235.                 <xsl:attribute name="bgcolor"><xsl:value-of select="$rowColor"/></xsl:attribute>
  236.                 <font size="4">
  237.                     <xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
  238.                 </font>
  239.             </td>
  240.             <xsl:variable name="alignData">
  241.                 <xsl:choose>
  242.                     <xsl:when test="$type = 'NUMBER'">
  243.                         <xsl:value-of select="'middle'"/>
  244.                     </xsl:when>
  245.                     <xsl:otherwise>
  246.                         <xsl:value-of select="'left'"/>
  247.                     </xsl:otherwise>
  248.                 </xsl:choose>
  249.             </xsl:variable>
  250.             <xsl:for-each select="fmp:COL">
  251.                 <xsl:choose>
  252.                     <xsl:when test="position() = 1"/>
  253.                     <xsl:when test="position() = last()">
  254.                         <td valign="middle">
  255.                             <xsl:attribute name="align"><xsl:value-of select="$alignData"/></xsl:attribute>
  256.                             <xsl:attribute name="bgcolor"><xsl:value-of select="$rowColor"/></xsl:attribute>
  257.                             <xsl:choose>
  258.                                 <xsl:when test="$type = 'NUMBER'">
  259.                                     <xsl:text/>
  260.                                 </xsl:when>
  261.                                 <xsl:otherwise>
  262.                                     <xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
  263.                                 </xsl:otherwise>
  264.                             </xsl:choose>
  265.                             <xsl:value-of select="fmp:DATA"/>
  266.                         </td>
  267.                     </xsl:when>
  268.                     <xsl:otherwise>
  269.                         <td align="left" valign="middle">
  270.                             <xsl:attribute name="bgcolor"><xsl:value-of select="$rowColor"/></xsl:attribute>
  271.                             <xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
  272.                             <xsl:value-of select="fmp:DATA"/>
  273.                         </td>
  274.                     </xsl:otherwise>
  275.                 </xsl:choose>
  276.             </xsl:for-each>
  277.         </tr>
  278.     </xsl:template>
  279. <!--
  280. Template: groupRow
  281.     
  282. Start each group with a row that spans the entire table 
  283. and is shaded differently.
  284. -->
  285.     <xsl:template name="groupRow">
  286.         <tr>
  287.             <td align="left">
  288.                 <xsl:attribute name="bgcolor"><xsl:value-of select="$groupColor"/></xsl:attribute>
  289.                 <xsl:attribute name="colspan"><xsl:value-of select="$dbNumCols"/></xsl:attribute>
  290.                 <font size="4">
  291.                     <xsl:text disable-output-escaping="yes">&nbsp;&nbsp;</xsl:text>
  292.                     <xsl:value-of select="fmp:COL[1]/fmp:DATA"/>
  293.                 </font>
  294.             </td>
  295.         </tr>
  296.     </xsl:template>
  297. <!--
  298. Template: subSumRow
  299.     
  300. Either counts or totals the members of each group.
  301. -->
  302.     <xsl:template name="subSumRow">
  303.         <tr>
  304.             <td align="right" valign="middle">
  305.                 <xsl:attribute name="bgcolor"><xsl:value-of select="$rowColor"/></xsl:attribute>
  306.                 <xsl:attribute name="colspan"><xsl:value-of select="$dbNumCols - 1"/></xsl:attribute>
  307.                 <xsl:choose>
  308.                     <xsl:when test="$type = 'NUMBER'">
  309.                         <xsl:text>Total:</xsl:text>
  310.                     </xsl:when>
  311.                     <xsl:otherwise>
  312.                         <xsl:text>Count:</xsl:text>
  313.                     </xsl:otherwise>
  314.                 </xsl:choose>
  315.             </td>
  316.             <td align="middle" valign="middle">
  317.                 <xsl:attribute name="bgcolor"><xsl:value-of select="$rowColor"/></xsl:attribute>
  318.                 <xsl:variable name="result">
  319.                     <xsl:choose>
  320.                         <xsl:when test="$type = 'NUMBER'">
  321.                             <xsl:value-of select="sum($currentGroup/fmp:COL[last()]/fmp:DATA)"/>
  322.                         </xsl:when>
  323.                         <xsl:otherwise>
  324.                             <xsl:value-of select="count($currentGroup/fmp:COL[1]/fmp:DATA)"/>
  325.                         </xsl:otherwise>
  326.                     </xsl:choose>
  327.                 </xsl:variable>
  328.                 <font size="4">
  329.                     <xsl:value-of select="$result"/>
  330.                 </font>
  331.             </td>
  332.         </tr>
  333.         <tr>
  334.             <td>
  335.                 <xsl:attribute name="colspan"><xsl:value-of select="$dbNumCols"/></xsl:attribute>
  336.                 <xsl:attribute name="bgcolor"><xsl:value-of select="$rowColor"/></xsl:attribute>
  337.                 <font size="4">
  338.                     <xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
  339.                 </font>
  340.             </td>
  341.         </tr>
  342.     </xsl:template>
  343. </xsl:stylesheet>
  344.